using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using SumerCoreDevOps.Application.GitTriggers.Commands;
using SumerCoreDevOps.Application.GitTriggers.Queries;
using SumerCoreDevOps.Shared.Models;
using SumerCoreDevOps.Application.Dtos.Requests.GitTriggers;
namespace SumerCoreDevOps.ControlApi.Controllers;

/// <summary>
/// Git 触发规则管理接口
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class GitTriggerController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<GitTriggerController> _logger;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="mediator">MediatR 中介者</param>
    /// <param name="logger">日志记录器</param>
    public GitTriggerController(IMediator mediator, ILogger<GitTriggerController> logger)
    {
        _mediator = mediator;
        _logger = logger;
    }

    /// <summary>
    /// 创建 Git 触发规则
    /// </summary>
    /// <param name="request">创建触发规则请求</param>
    /// <returns>返回新创建的触发规则对象，包括规则ID、名称、触发类型、分支模式等信息</returns>
    /// <response code="200">成功创建触发规则</response>
    [HttpPost]
    public async Task<IActionResult> CreateTriggerRule([FromBody] CreateGitTriggerRuleRequest request)
    {
        var command = new CreateGitTriggerRuleCommand(
            request.WorkflowId,
            request.Name,
            request.TriggerType,
            request.BranchPattern,
            request.TagPattern,
            request.PathFilter,
            User.Identity?.Name);

        var result = await _mediator.Send(command);
        return Ok(ApiResponse.Ok(result));
    }

    /// <summary>
    /// 更新 Git 触发规则
    /// </summary>
    /// <param name="id">触发规则ID</param>
    /// <param name="request">更新触发规则请求</param>
    /// <returns>返回操作成功响应</returns>
    /// <response code="200">成功更新触发规则</response>
    [HttpPut("{id}")]
    public async Task<IActionResult> UpdateTriggerRule(long id, [FromBody] UpdateGitTriggerRuleRequest request)
    {
        var command = new UpdateGitTriggerRuleCommand(
            id,
            request.Name,
            request.TriggerType,
            request.BranchPattern,
            request.TagPattern,
            request.PathFilter,
            request.IsEnabled);

        await _mediator.Send(command);
        return Ok(ApiResponse.Ok(null));
    }

    /// <summary>
    /// 删除 Git 触发规则
    /// </summary>
    /// <param name="id">触发规则ID</param>
    /// <returns>返回操作成功响应</returns>
    /// <response code="200">成功删除触发规则</response>
    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteTriggerRule(long id)
    {
        await _mediator.Send(new DeleteGitTriggerRuleCommand(id));
        return Ok(ApiResponse.Ok(null));
    }

    /// <summary>
    /// 获取工作流的所有触发规则
    /// </summary>
    /// <param name="workflowId">工作流ID</param>
    /// <returns>返回该工作流配置的所有触发规则列表</returns>
    /// <response code="200">成功返回触发规则列表</response>
    [HttpGet("workflow/{workflowId}")]
    public async Task<IActionResult> GetTriggerRulesByWorkflow(long workflowId)
    {
        var rules = await _mediator.Send(new GetGitTriggerRulesByWorkflowQuery(workflowId));
        return Ok(ApiResponse.Ok(rules));
    }

    /// <summary>
    /// 根据 ID 获取触发规则详情
    /// </summary>
    /// <param name="id">触发规则ID</param>
    /// <returns>返回触发规则详细信息，如果不存在返回404错误</returns>
    /// <response code="200">成功返回触发规则</response>
    /// <response code="404">触发规则不存在</response>
    [HttpGet("{id}")]
    public async Task<IActionResult> GetTriggerRuleById(long id)
    {
        var rule = await _mediator.Send(new GetGitTriggerRuleByIdQuery(id));
        if (rule == null)
        {
            return NotFound(ApiResponse.Fail($"触发规则 {id} 不存在"));
        }
        return Ok(ApiResponse.Ok(rule));
    }
}
